<?php

namespace app\astrologer\controller\v7;

use app\astrologer\controller\AstrAuth;
use app\common\cachemodel\ZlcrAstrPush;
use app\common\lib\exception\ApiException;
use think\response\Json;

class Push extends AstrAuth
{
    /**
     * 推送绑定
     * @return Json
     * @throws ApiException
     */
    public function bind()
    {
        $regid = input('regid/s', '');
        $onlyCode = input('only_code/s', '');
        $notice_status = input('notice_status/d', -1);
        $push_type = input('push_type/d', 1);
        if (!in_array($notice_status, [0, 1]))
        {
            return show(config('code.error'), '请求异常，请稍后再试～');
        }
        $version = $this->headers['version'] ?? '';
        $model = $this->headers['model'] ?? '';
        $appType = $this->headers['app-type'] === 'android' ? 1 : 2; // 手机类型 1安卓 2IOS

        if (empty($regid))
        {
            return show(config('code.error'), 'regid不能为空');
        }

        if ($this->is_simulated)
        {   // 模拟登录 不进行推送设备绑定
            return show(config('code.success'), '绑定成功');
        }

        if ($appType === 2)
        {  // ios
            $push_type = 6; // 6=>ios
        }

        $data = [
            'status' => $notice_status,
            'regid' => $regid,
            'push_type' => $push_type,
            'version' => 'V' . $version,
            'model' => $model,
            'type' => $appType,
            'only_code' => $onlyCode,
        ];

        (new ZlcrAstrPush())->pushBindNew($data, (int)$this->astrInfo['id']);

        return show(config('code.success'), '绑定成功');
    }

    /**
     * 获取推送状态
     */
    public function getNoticeInfo()
    {
        $astrPushModel = new ZlcrAstrPush();
        //获取用户推送信息
        $userPush = $astrPushModel->getUserRegid($this->astrInfo['id']);
        $pushStatus = $userPush['status'] ?? 1;
        if ($pushStatus === 2)
        {
            $pushStatus = 1;
        }
        $data = [
            'push_info' => [ // 个性化推送设置
                'title' => '接收推送消息',
                'name' => '开启后可及时接收咨询师消息',
                'status' => $pushStatus,
                'type' => 1,
            ],
            'message_list' => [
                [
                    'type' => 2,
                    'name' => '聊天提醒',
                    'status' => $userPush['msg_status'] ?? 1,
                ],
            ],
        ];

        return show(config('code.success'), '获取成功', $data);
    }

    /**
     * 设置推送状态
     */
    public function setNoticeStatus()
    {
        $type = input('type/d', 0);
        $status = input('status/d', -1);
        if (!in_array($type, [1, 2], true) || !in_array($status, [1, 0], true))
        {
            return show(config('code.error'), '请求异常，请稍后再试～');
        }

        $data = [];
        switch ($type)
        {
            case 1: // 接收推送消息
                $data = [
                    'status' => $status,
                ];
                break;
            case 2: // 聊天提醒
                $data = [
                    'msg_status' => $status,
                ];
                break;
        }

        $astrPushModel = new ZlcrAstrPush();

        $astrPushModel->pushBindNew($data, $this->astrInfo['id']);

        return show(config('code.success'), '操作成功');
    }
}